package com.ruoyi.equ.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.equ.domain.EquInspection;
import com.ruoyi.equ.domain.EquInsplan;
import com.ruoyi.equ.service.IEquBasicService;
import com.ruoyi.equ.service.IEquInspectionService;
import com.ruoyi.equ.service.IEquInsplanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备送检详情Controller
 * 
 * @author luguanqing
 * @date 2020-10-29
 */
@RestController
@RequestMapping("/equ/insplan")
public class EquInsplanController extends BaseController
{
    @Autowired
    private IEquInsplanService equInsplanService;

    @Autowired
    private IEquBasicService equBasicService;

    @Autowired
    private IEquInspectionService equInspectionService;

    /**
     * 查询设备送检详情列表
     */
    @GetMapping("/list")
    public TableDataInfo list(EquInsplan equInsplan)
    {
        startPage();
        List<EquInsplan> list = equInsplanService.selectEquInsplanList(equInsplan);
        return getDataTable(list);
    }

    /**
     * 导出设备送检详情列表
     */
    @Log(title = "设备送检详情", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(EquInsplan equInsplan)
    {
        List<EquInsplan> list = equInsplanService.selectEquInsplanList(equInsplan);
        ExcelUtil<EquInsplan> util = new ExcelUtil<EquInsplan>(EquInsplan.class);
        return util.exportExcel(list, "insplan");
    }

    /**
     * 获取设备送检详情详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo( @PathVariable("id") String id)
    {
        return AjaxResult.success(equInsplanService.selectEquInsplanById(id));
    }

    /**
     * 根据送检批次编号获取设备送检详情详细信息
     */
    @GetMapping("/getByInsId/{id}")
    public AjaxResult getByInsId(@PathVariable("id") String id)
    {
        return AjaxResult.success(equInsplanService.selectEquInsplanByInsId(id));
    }

    /**
     * 新增设备送检详情
     */
    @Log(title = "设备送检详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EquInsplan equInsplan)
    {
        return toAjax(equInsplanService.insertEquInsplan(equInsplan));
    }

    /**
     * 修改设备送检详情
     */
    @Log(title = "设备送检详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EquInsplan equInsplan)
    {
        return toAjax(equInsplanService.updateEquInsplan(equInsplan));
    }

    /**
     * 修改设备送检证书到期
     */
    @GetMapping(value = "/editEquInvalidity/{id}")
    public AjaxResult editEquInvalidity(@PathVariable("id") String id)
    {
        //变更计划列表到期日期
        EquInsplan equInsplan = new EquInsplan();
        equInsplan.setInsId(id);
        equInsplan.setEquInvalidity(DateUtils.getNowDate());
        int ret = equInsplanService.updateEquInvalidity(equInsplan);
        //变更送检计划状态
        EquInspection equInspection = new EquInspection();
        equInspection.setInsState("2");
        equInspection.setId(id);
        equInspectionService.updateEquInspection(equInspection);
        //变更设备基础信息证书到期信息
        if(ret > 0){
            equBasicService.updateEquInvalidity();
        }
        return toAjax(ret);
    }

    /**
     * 删除设备送检详情
     */
    @Log(title = "设备送检详情", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(equInsplanService.deleteEquInsplanByIds(ids));
    }
}
